
*******************************************************
* (1.0) Load the analysis sample - mother-year panel
*******************************************************
	use 	"$f_2PKmotherpanel", clear	
	
	gen svydate		= (88*12)+7
		
	cap drop _merge
	merge m:1 prov year using `"$d_data\ProductionData.dta"'
	
	cap drop _merge
	merge m:1 prov year using `"$d_data\IMR.dta"'

	drop if year<year_marriage	
	
	************************************************
	* (1.1) Change LLF implementation dates to 
	*		Chen and Huang (2020)
	************************************************

	replace LLFyear = 1972 if prov == 34 
	replace LLFyear = 1972 if prov == 35
	replace LLFyear = 1974 if prov == 45
	replace LLFyear = 1975 if prov == 52
	replace LLFyear = 1973 if prov == 41
	replace LLFyear = 1971 if prov == 42
	replace LLFyear = 1971 if prov == 43
	replace LLFyear = 1972 if prov == 15
	replace LLFyear = 1973 if prov == 32
	replace LLFyear = 1972 if prov == 22
	replace LLFyear = 1972 if prov == 21
	replace LLFyear = 1972 if prov == 64
	replace LLFyear = 1974 if prov == 63
	replace LLFyear = 1971 if prov == 61
	replace LLFyear = 1970 if prov == 37
	
	drop years_since_LLF 
	gen years_since_LLF = year - LLFyear	
	drop if years_since_LLF < -9 | years_since_LLF > 8
	drop if year >=1980
	drop if year <1964	
	
	forval i = 1/9 {
		gen 	years_since_LLFpre`i' 	= 0 if years_since_LLF != . 
		replace years_since_LLFpre`i' 	= 1 if years_since_LLF == -`i'
 	}

		gen 	years_since_LLFpost0 	= 0 if years_since_LLF != . 
		replace years_since_LLFpost0 	= 1 if years_since_LLF == 0 	

	forval i = 1/9 {
		gen 	years_since_LLFpost`i' 	= 0 if years_since_LLF != . 
		replace years_since_LLFpost`i' 	= 1 if years_since_LLF == `i'
 	}
	
	gen son = hasson 
	gen noson = 1 if hasson == 0 
	replace noson = 0 if hasson == 1
	
		forval j = 1/9 { 
			gen noson_pre_`j' = years_since_LLFpre`j' *noson
			} 
		forval j = 0/9 { 
			gen noson_p_post_`j' = years_since_LLFpost`j'*noson 
			}
	
	*group 4th births and higher (parity 3 moms) 
	replace parity = 3 if parity >=3 & parity != . 
	
	forval i = 1/3 { 
		gen p_`i' = 0 if parity != . 
		replace p_`i' = 1 if parity == `i'
		gen year_p`i'= year*p_`i'
		} 
		
	* make interactions parity x years since LLF leaving out parity 0 moms
	forval i = 1/3 { 
		
		forval j = 1/9 { 
			gen p`i'_LLFpre`j' = 0 if years_since_LLF != . & parity != . 
			replace p`i'_LLFpre`j' = 1 if years_since_LLF == -`j' & parity == `i' 
			} 
			
			gen p`i'_LLFpost0 = 0 if years_since_LLF != . & parity != . 
			replace p`i'_LLFpost0 = 1 if years_since_LLF == 0 & parity == `i' 
			
		forval j = 1/9 { 
			gen p`i'_LLFpost`j' = 0 if years_since_LLF != . & parity != . 
			replace p`i'_LLFpost`j' = 1 if years_since_LLF == `j' & parity == `i' 
			} 
		}	
		
	* make interactions parity x years since LLF x sons leaving out parity 0 moms
	forval i = 1/3 { 
		
		forval j = 1/9 { 
			gen p`i'_LLFpre`j'_son =  p`i'_LLFpre`j'*hasson 
			} 
			
			gen p`i'_LLFpost0_son = p`i'_LLFpost0*hasson 
			
		forval j = 1/9 { 
			gen p`i'_LLFpost`j'_son =  p`i'_LLFpost`j'*hasson
			} 
		}
	
	forval j = 1/9 { 
		gen pre_`j'_son = years_since_LLFpre`j' *hasson
		} 
	forval j = 0/9 { 
		gen post_`j'_son = years_since_LLFpost`j'*hasson 
		}
			
	gen p1_son = p_1*hasson
	gen p2_son = p_2*hasson
	gen p3_son = p_3*hasson
	
	cap drop _merge
	merge m:1 prov using `"$d_data\sentdown_mean.dta"'
	drop _merge 
	
	gen 	culturalrev = 0 
	replace culturalrev = mean_sentdown if year >= 1966 & year <=1969

*****************************************************
* (2.0) Gen Stacked Data 
*****************************************************

	do `"$d_do\LLF_StackData.do"'

	gen years_since_LLF = year - LLFyear 
	forval i = 1/9 {
		gen 	years_since_LLFpre`i' 	= 0 if years_since_LLF != . 
		replace years_since_LLFpre`i' 	= 1 if years_since_LLF == -`i'
 	}

		gen 	years_since_LLFpost0 	= 0 if years_since_LLF != . 
		replace years_since_LLFpost0 	= 1 if years_since_LLF == 0 	

	forval i = 1/9 {
		gen 	years_since_LLFpost`i' 	= 0 if years_since_LLF != . 
		replace years_since_LLFpost`i' 	= 1 if years_since_LLF == `i'
 	}
		
		forval j = 1/9 { 
			gen noson_pre_`j' = years_since_LLFpre`j' *noson
			} 
		forval j = 0/9 { 
			gen noson_p_post_`j' = years_since_LLFpost`j'*noson 
			}
		
	* make interactions parity x years since LLF leaving out parity 0 moms
	forval i = 1/3 { 
		
		forval j = 1/9 { 
			gen p`i'_LLFpre`j' = 0 if years_since_LLF != . & parity != . 
			replace p`i'_LLFpre`j' = 1 if years_since_LLF == -`j' & parity == `i' 
			} 
			
			gen p`i'_LLFpost0 = 0 if years_since_LLF != . & parity != . 
			replace p`i'_LLFpost0 = 1 if years_since_LLF == 0 & parity == `i' 
			
		forval j = 1/9 { 
			gen p`i'_LLFpost`j' = 0 if years_since_LLF != . & parity != . 
			replace p`i'_LLFpost`j' = 1 if years_since_LLF == `j' & parity == `i' 
			} 
		}	
		
	* make interactions parity x years since LLF x sons leaving out parity 0 moms
	forval i = 1/3 { 
		
		forval j = 1/9 { 
			gen p`i'_LLFpre`j'_son =  p`i'_LLFpre`j'*hasson 
			} 
			
			gen p`i'_LLFpost0_son = p`i'_LLFpost0*hasson 
			
		forval j = 1/9 { 
			gen p`i'_LLFpost`j'_son =  p`i'_LLFpost`j'*hasson
			} 
		}
	
	forval j = 1/9 { 
		gen pre_`j'_son = years_since_LLFpre`j' *hasson
		} 
	forval j = 0/9 { 
		gen post_`j'_son = years_since_LLFpost`j'*hasson 
		}
			
	global controls1 	"m_year_birth i.educ age_marriage U5MR_5yr_ave"
	global controls2 	"grain agric_production gdp pop_total_agric"
	
	order m_year_birth-year  years_since_LLFpre9 years_since_LLFpre8 years_since_LLFpre7 years_since_LLFpre6 ///
		years_since_LLFpre5 years_since_LLFpre4 years_since_LLFpre3 years_since_LLFpre2 years_since_LLFpre1 ///
		years_since_LLFpost0 years_since_LLFpost1 years_since_LLFpost2 years_since_LLFpost3 years_since_LLFpost4 years_since_LLFpost5 ///
		years_since_LLFpost6 years_since_LLFpost7 years_since_LLFpost8 years_since_LLFpost9  ///
		pre_9_son pre_8_son pre_7_son pre_6_son pre_5_son pre_4_son pre_3_son pre_2_son ///
		post_0_son post_1_son post_2_son post_3_son post_4_son post_5_son post_6_son post_7_son post_8_son post_9_son ///
		p1_LLFpre9 p1_LLFpre8 p1_LLFpre7 p1_LLFpre6 p1_LLFpre5 p1_LLFpre4 p1_LLFpre3 p1_LLFpre2 p1_LLFpre1 ///
		p1_LLFpost0 p1_LLFpost1 p1_LLFpost2 p1_LLFpost3 p1_LLFpost4 p1_LLFpost5 p1_LLFpost6 p1_LLFpost7 p1_LLFpost8 p1_LLFpost9 ///
		p2_LLFpre9 p2_LLFpre8 p2_LLFpre7 p2_LLFpre6 p2_LLFpre5 p2_LLFpre4 p2_LLFpre3 p2_LLFpre2 p2_LLFpre1 ///
		p2_LLFpost0 p2_LLFpost1 p2_LLFpost2 p2_LLFpost3 p2_LLFpost4 p2_LLFpost5 p2_LLFpost6 p2_LLFpost7 p2_LLFpost8 p2_LLFpost9 ///
		p3_LLFpre9 p3_LLFpre8 p3_LLFpre7 p3_LLFpre6 p3_LLFpre5 p3_LLFpre4 p3_LLFpre3 p3_LLFpre2 p3_LLFpre1 ///
		p3_LLFpost0 p3_LLFpost1 p3_LLFpost2 p3_LLFpost3 p3_LLFpost4 p3_LLFpost5 p3_LLFpost6 p3_LLFpost7 p3_LLFpost8 p3_LLFpost9 ///
		p1_LLFpre9_son p1_LLFpre8_son p1_LLFpre7_son p1_LLFpre6_son p1_LLFpre5_son p1_LLFpre4_son p1_LLFpre3_son p1_LLFpre2_son p1_LLFpre1_son ///
		p1_LLFpost0_son p1_LLFpost1_son p1_LLFpost2_son p1_LLFpost3_son p1_LLFpost4_son p1_LLFpost5_son p1_LLFpost6_son p1_LLFpost7_son p1_LLFpost8_son p1_LLFpost9_son ///
		p2_LLFpre9_son p2_LLFpre8_son p2_LLFpre7_son p2_LLFpre6_son p2_LLFpre5_son p2_LLFpre4_son p2_LLFpre3_son p2_LLFpre2_son p2_LLFpre1_son ///
		p2_LLFpost0_son p2_LLFpost1_son p2_LLFpost2_son p2_LLFpost3_son p2_LLFpost4_son p2_LLFpost5_son p2_LLFpost6_son p2_LLFpost7_son p2_LLFpost8_son p2_LLFpost9_son ///
		p3_LLFpre9_son p3_LLFpre8_son p3_LLFpre7_son p3_LLFpre6_son p3_LLFpre5_son p3_LLFpre4_son p3_LLFpre3_son p3_LLFpre2_son p3_LLFpre1_son ///
		p3_LLFpost0_son p3_LLFpost1_son p3_LLFpost2_son p3_LLFpost3_son p3_LLFpost4_son p3_LLFpost5_son p3_LLFpost6_son p3_LLFpost7_son p3_LLFpost8_son p3_LLFpost9_son 

	gen p1_son = p_1*hasson
	gen p2_son = p_2*hasson
	gen p3_son = p_3*hasson
		
	drop if years_since_LLF>7
	char FE_prov[omit] 108 
	drop if years_since_LLF<-8

	*****************************************************
	* (2.1) Regression
	*****************************************************
	xi: reg delivered years_since_LLFpre8-years_since_LLFpre2 years_since_LLFpost0-years_since_LLFpost7 ///
				pre_8_son-pre_2_son post_0_son-post_7_son ///
				p1_LLFpre8-p1_LLFpre2 p1_LLFpost0-p1_LLFpost7  ///
				p2_LLFpre8-p2_LLFpre2 p2_LLFpost0-p2_LLFpost7 ///
				p2_LLFpre8_son-p2_LLFpre2_son p2_LLFpost0_son-p2_LLFpost7_son ///
				p3_LLFpre8-p3_LLFpre2 p3_LLFpost0-p3_LLFpost7 ///
				p3_LLFpre8_son-p3_LLFpre2_son p3_LLFpost0_son-p3_LLFpost7_son ///
				p_1 p_2 p_3 hasson  p2_son p3_son culturalrev $controls1 $controls2 i.FE_t i.FE_prov ///
				if year!=1967 & year !=1968 & years_since_LLF<8, cluster(prov)
			
	************************************************
	* (2.2) Preserve the Coefficients
	************************************************
	
		mat coef = e(b)
		mat temp = J(1,1, .) 
		
		local j = 1
		
		forval i = 2/8 {	
			local k = abs(`i' - 10)
			lincom p`j'_LLFpre`k' + pre_`k'_son 
			local est = r(estimate)
			
			mat temp1 = coef[1,"p`j'_LLFpre`k'"]
			mat temp2 = `est'
			mat temp = temp \ temp1 \ temp2
			}
			
		forval i = 9/16 {
			local k = abs(`i' - 9)
			lincom p`j'_LLFpost`k' + post_`k'_son 
			local est = r(estimate)
	
			mat temp1 = coef[1,"p`j'_LLFpost`k'"]
			mat temp2 = `est'
			mat temp = temp \ temp1 \ temp2
			}
		
		forval j = 2/3 {
		
		forval i = 2/8 {	
			local k = abs(`i' - 10)
			lincom p`j'_LLFpre`k' + pre_`k'_son + p`j'_LLFpre`k'_son
			local est = r(estimate)
	
			mat temp1 = coef[1,"p`j'_LLFpre`k'"]
			mat temp2 = `est'
			mat temp = temp \ temp1 \ temp2
			}
			
		forval i = 9/16 {
			local k = abs(`i' - 9)
			lincom p`j'_LLFpost`k' + post_`k'_son + p`j'_LLFpost`k'_son
			local est = r(estimate)
	
			mat temp1 = coef[1,"p`j'_LLFpost`k'"]
			mat temp2 = `est'
			mat temp = temp \ temp1 \ temp2
			}
		} 
					
		forval i = 2/8 {	
			local k = abs(`i' - 10)
	
			mat temp1 = coef[1,"years_since_LLFpre`k'"]
			mat temp = temp \ temp1
			}
			
		forval i = 9/16 {
			local k = abs(`i' - 9)
	
			mat temp1 = coef[1,"years_since_LLFpost`k'"]
			mat temp = temp \ temp1
			}
		
		mat checkdates = results, temp
		
	
	mat colnames checkdates = "EventYear" "Parity" "Son" "Main" "cil" "ciu" "CH2020"  

	
	************************************************
	* (2.3) Plot Results
	************************************************
	clear 
	svmat checkdates, name(col)
			
	replace EventYear = EventYear - 9 
	replace EventYear = EventYear - 1 if EventYear <= -1
		
	replace CH2020  = CH2020*100
		
	replace cil = cil*100
	replace ciu  = ciu*100
	replace Main = Main*100
	
	drop if EventYear < -8 
	drop if EventYear > 8 

	gen yline = 0 
	replace Parity = Parity + 1

		*****************************
		* (2.3.2) Second Parity Births
		*****************************
				
		replace EventYear = EventYear + .15 	if Son == 1
		replace EventYear = EventYear - .15 	if Son == 0
		
		twoway 	///
					(rcap cil ciu EventYear if Parity == 2 & Son==1, lcolor(blue))   ///
					(scatter Main EventYear if Parity == 2 & Son==1, msymbol(circle) mcolor(blue) lcolor(blue) cmissing(n)) ///
					(scatter CH2020 EventYear if Parity == 2 & Son==1, msymbol(circle_hollow) mcolor(blue) lcolor(blue) cmissing(n)) ///
					(rcap cil ciu EventYear if Parity == 2 & Son==0, lcolor(red))   ///
					(scatter Main EventYear if Parity == 2 & Son==0, msymbol(circle) mcolor(red) lcolor(red) cmissing(n)) ///
					(scatter CH2020 EventYear if Parity == 2 & Son==0, msymbol(circle_hollow) mcolor(red) lcolor(red) cmissing(n)) ///
					(line yline EventYear if Parity == 2 & Son==., lcolor(black) cmissing(n) lpattern(solid)) ///
					(pci -24 -0.5 12 -0.5, lcolor(black)) ///
					,  ytitle("Percentage Point" "Change in Probability" ) ///
					xtitle("Years Since LLF Policy") yline(0, lcolor(black)) xline(-.5, lcolor(black)) xlab(-8(1)7)  ylab(-24(4)12) ///
					legend(off) title("Second Order Births")

			graph export `"$d_fig\Fig_A9d_PPR.jpg"', replace
			
		*****************************
		* (2.3.3) Third Order Births
		*****************************
	
		twoway 	///
					(rcap cil ciu EventYear if Parity == 3 & Son==1, lcolor(blue))   ///
					(scatter Main EventYear if Parity == 3 & Son==1, msymbol(circle) mcolor(blue) lcolor(blue) cmissing(n)) ///
					(scatter CH2020 EventYear if Parity == 3 & Son==1, msymbol(circle_hollow) mcolor(blue) lcolor(blue) cmissing(n)) ///
					(rcap cil ciu EventYear if Parity == 3 & Son==0, lcolor(red))   ///
					(scatter Main EventYear if Parity == 3 & Son==0, msymbol(circle) mcolor(red) lcolor(red) cmissing(n)) ///
					(scatter CH2020 EventYear if Parity == 3 & Son==0, msymbol(circle_hollow) mcolor(red) lcolor(red) cmissing(n)) ///
					(line yline EventYear if Parity == 3 & Son==., lcolor(black) cmissing(n) lpattern(solid)) ///
					(pci -24 -0.5 12 -0.5, lcolor(black)) ///
					,  ytitle("Percentage Point" "Change in Probability" ) ///
					xtitle("Years Since LLF Policy") yline(0, lcolor(black)) xline(-.5, lcolor(black)) xlab(-8(1)7)  ylab(-24(4)12) ///
					legend(off) title("Third Order Births")

			graph export `"$d_fig\Fig_A10d_PPR.jpg"', replace

		*****************************
		* (2.3.4) Fourth Order Births
		*****************************
		
		twoway 	///
					(rcap cil ciu EventYear if Parity == 4 & Son==1, lcolor(blue))   ///
					(scatter Main EventYear if Parity == 4 & Son==1, msymbol(circle) mcolor(blue) lcolor(blue) cmissing(n)) ///
					(scatter CH2020 EventYear if Parity == 4 & Son==1, msymbol(circle_hollow) mcolor(blue) lcolor(blue) cmissing(n)) ///
					(rcap cil ciu EventYear if Parity == 4 & Son==0, lcolor(red))   ///
					(scatter Main EventYear if Parity == 4 & Son==0, msymbol(circle) mcolor(red) lcolor(red) cmissing(n)) ///
					(scatter CH2020 EventYear if Parity == 4 & Son==0, msymbol(circle_hollow) mcolor(red) lcolor(red) cmissing(n)) ///
					(line yline EventYear if Parity == 4 & Son==., lcolor(black) cmissing(n) lpattern(solid)) ///
					(pci -24 -0.5 12 -0.5, lcolor(black)) ///
					,  ytitle("Percentage Point" "Change in Probability" ) ///
					xtitle("Years Since LLF Policy") yline(0, lcolor(black)) xline(-.5, lcolor(black)) xlab(-8(1)7)  ylab(-24(4)12) ///
					legend(off) title("Fourth Order Births")

			graph export `"$d_fig\Fig_A11d_PPR.jpg"', replace
			
		restore	


